﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="DocumentStoreHolder.cs" company="Radio Comeback">
//   
// </copyright>
// <summary>
//   Defines the DocumentStoreHolder type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace TinyRadio.Web.Controllers
{
    using System;
    using System.Configuration;

    using Raven.Client;
    using Raven.Client.Document;

    /// <summary>The document store holder.</summary>
    public static class DocumentStoreHolder
    {
        /// <summary>The document store.</summary>
        private static IDocumentStore documentStore;

        /// <summary>Gets the document store.</summary>
        public static IDocumentStore DocumentStore
        {
            get
            {
                if (documentStore != null)
                {
                    return documentStore;
                }

                lock (typeof(DocumentStoreHolder))
                {
                    if (documentStore != null)
                    {
                        return documentStore;
                    }

                    documentStore = new DocumentStore
                    {
                        ConnectionStringName = ConnectionStringName
                    };


                    documentStore.Initialize();

                    //IndexCreation.CreateIndexes(typeof(RadioStation).Assembly, _documentStore);
                }

                return documentStore;
            }
        }

        /// <summary>Gets the connection string name.</summary>
        private static string ConnectionStringName
        {
            get
            {
                var customConnection = ConfigurationManager.ConnectionStrings[Environment.MachineName] != null;
                var connectionStringName = customConnection ? Environment.MachineName : "RavenDB";
                return connectionStringName;
            }
        }
    }
}